package com.atguigu.service.impl;

import com.atguigu.entity.PlatformPropertyKey;
import com.atguigu.entity.PlatformPropertyValue;
import com.atguigu.mapper.PlatformPropertyKeyMapper;
import com.atguigu.service.PlatformPropertyKeyService;
import com.atguigu.service.PlatformPropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlatformPropertyKeyServiceImpl extends ServiceImpl<PlatformPropertyKeyMapper, PlatformPropertyKey> implements PlatformPropertyKeyService {

    @Autowired
    private PlatformPropertyValueService platformPropertyValueService;

    @Override
    public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long baseCategory1Id, Long baseCategory2Id, Long baseCategory3Id) {
        return baseMapper.getPlatformPropertyByCategoryId(baseCategory1Id,baseCategory2Id,baseCategory3Id);
    }

    @Override
    public void savePlatformProperty(PlatformPropertyKey platformPropertyKey) {
        if(platformPropertyKey.getId() != null){
            //修改
            baseMapper.updateById(platformPropertyKey);
        }else{
            //添加
            baseMapper.insert(platformPropertyKey);
        }
        //删除value
        QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
        wrapper.eq("property_key_id",platformPropertyKey.getId());
        platformPropertyValueService.remove(wrapper);
        //添加value
        List<PlatformPropertyValue> propertyValueList = platformPropertyKey.getPropertyValueList();
        for (PlatformPropertyValue platformPropertyValue : propertyValueList) {
            platformPropertyValue.setPropertyKeyId(platformPropertyKey.getId());
        }
        platformPropertyValueService.saveBatch(propertyValueList);

    }

    @Override
    public List<PlatformPropertyKey> getPlatformPropertyBySkuId(long skuId) {
        return baseMapper.getPlatformPropertyBySkuId(skuId);
    }
}
